<template>
  <el-dialog
    v-el-drag-dialog
    title="销售人员列表"
    :visible.sync="visible"
    width="1024px"
    append-to-body
    destroy-on-close
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :inline="true" :model="query" label-position="right" label-width="80px">
      <el-form-item label="所属机构" prop="orgSNNo">
        <el-tree-select
          v-model="query.orgSNNo"
          :clearable="false"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
          }"
          :tree-params=" {
            'default-expand-all': true,
            'expand-on-click-node': false,
            'highlight-current': true,
            props:{
              value:'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
        />
      </el-form-item>
      <el-form-item label="人员名称" prop="userName">
        <el-input v-model="query.userName" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
    </el-form>
    <lb-table
      ref="lbTable"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      :page-size="10"
      :immediate-fetch="false"
      @fetch="fetchData"
      @row-click="clickRow"
      @row-dblclick="dblclickRow"
    />
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryMap, querySalesmanList } from '@/api'

  export default {
    name: 'salesman',
    props: {
      treeData: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        visible: false,
        query: {
          orgSNNo: '',
          userName: ''
        },
        tableData: {
          total: 0,
          column: [
            {
              prop: 'userCode',
              label: '编码'
            },
            {
              prop: 'userName',
              label: '名称'
            },
            {
              prop: 'utype',
              label: '类型',
              width: 160,
              render: (h, { row }) => {
                if (!row.utype) return ''
                const arr = this.userTypeMap.filter((item, index) => (row.utype >> index) & 1)
                const str = arr.map(i => i.desc).join(',')
                return <span>{str}</span>
              }
            },
            {
              prop: 'uphone',
              label: '电话',
              width: 120
            },
            {
              prop: 'orgName',
              label: '所属机构',
              width: 200
            },
            {
              prop: 'uaddress',
              label: '地址',
              width: 200
            }
          ],
          data: []
        },
        currentRow: null
      }
    },
    created() {
      this.queryOptionsMap()
    },
    methods: {
      async open() {
        this.fetchData({ currentPage: 1, pageSize: 10 })
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this.query,
          utype: 64,
          currentPage,
          pageSize
        }
        const res = await querySalesmanList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      // 查询枚举
      async queryOptionsMap() {
        const res = await queryMap({ enumName: 'UserType' })
        this.userTypeMap = res.data // 用户类型
      },
      clickRow(row) {
        if (this.currentRow && this.currentRow === row) {
          this.$refs.lbTable.setCurrentRow()
          this.currentRow = null
        } else {
          this.$refs.lbTable.setCurrentRow(row)
          this.currentRow = row
        }
      },
      // 双击行
      dblclickRow(row) {
        this.currentRow = row
        this.success()
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      success() {
        this.resolve(this.currentRow)
        this.visible = false
      },
      reset() {
        this.query = {
          orgSNNo: '',
          userName: ''
        }
      }
    }
  }
</script>

<style scoped>

</style>
